function updateInfoSubmit(data) {
    console.log('step into registerSubmit()')
    $(".loading").show();
    $.ajax({
        url: $.httpRootPath() + 'rest/user/updateInfo',
        type: 'POST',
        dataType: 'json',
        data: data,
        timeout: 15000,
        cache: false,
        success: function (r) {
            $(".loading").hide();
            if (r.code == "00") {
                errorToast("信息更新成功", 3);
            } else {
                errorToast(r.msg, 3);
                return;
            }
            if (fromPage != null && fromPage != "") {
                errorToast("更新成功,跳转至上个页面", 3);
                setTimeout(function () {
                    window.location.href = fromPage;
                }, 3000);
            } else {
                errorToast("更新成功", 3);
                setTimeout(function () {
                    window.location.href = "wxmp/user/info";
                }, 3000);
            }


        },
        error: function (e) {
            $(".loading").hide();
            ajaxErrorEvent(e);
        }
    });
}


function loginSubmit(username, psw) {
    console.log('step into loginSubmit()')
    if (username == null || psw == null) {
        return false;
    }
    ;
    var data = {
        username: username,
        password: psw,
        code: code,
    };
    $(".loading").show();
    $.ajax({
        url: $.httpRootPath() + 'rest/user/login',
        type: 'POST',
        dataType: 'json',
        data: data,
        timeout: 15000,
        cache: false,
        success: function (r) {
            $(".loading").hide();
            //05 说明信息不全
            if (r.code != "00" && r.code != "05") {
                errorToast(r.msg, 3);
                return false;
            }
            errorToast(r.msg + "</br>即将跳转", 1);
            setTimeout(function () {
                window.location.href = "wxmp/user/info";
            }, 3000);
        },
        error: function (e) {
            $(".loading").hide();
            ajaxErrorEvent(e);
        }
    });
}

function getUserInfo(callback) {
    $(".loading").show();
    $.ajax({
        url: $.httpRootPath() + 'rest/user/getInfo',
        type: 'GET',
        dataType: 'json',
        timeout: 15000,
        success: function (r) {
            $(".loading").hide();
            if (r.code == "00" || r.code == "05") {
                var obj = r.data;
                var mobileList = obj.mobiles.split("/");
                $("input[name='memberNum']").val(obj.memberNum);
                $("input[name='name']").val(obj.name);
                $("input[name='wechat']").val(obj.weChat);
                $("input[name='email']").val(obj.email);
                $("input[name='addr']").val(obj.site);
                $("input[name='introduce']").val(obj.introduce);
                $("input[name='tel']").val(obj.tel);
                $("input[name='birthday']").val(obj.birthday);
                $("input[name='mobile']").val(mobileList[0]);
                // $("input[name='sex'][value='"+obj.sex+"']").attr("checked", true);
                // $("#sex option[value='"+obj.sex+"']").attr("selected","selected");
                if (obj.picture != "") {
                    $('#filePicker').attr('src', r.data.picture);
                }

                for (var i = 1; i < mobileList.length; i++) {
                    var str = '<p class="weight_out por">' +
                        '         <span onclick= "removeMobile(this)"><img src="img/mobile1.png" id="addMobile"></span>手机' +
                        '         <input type="text" placeholder="必填" class="weight" maxlength="11" value= ' + mobileList[i] +
                        ' name="mobile"></p>'
                    $("#mobileTop").before(str);
                }
                if (r.code == "05") {
                    errorToast("请先完善或确认信息!", 3)
                }
            } else {
                errorToast("获取信息失败:" + r.msg, 3)
            }
        },
        error: function (e) {
            $(".loading").hide();
            ajaxErrorEvent(e);
        },
        complete: function () {
            callback();
        }
    });
}

function setHisInfo(r, callback) {
    if (r.code == '00') {
        var obj = r.data;
        obj.mobiles = obj.mobiles.split("/");
        for (var key in obj) {
            $(':input[name=' + key + ']').val(obj[key]);
        }
        $("input[name='addr']").val(obj.site);
        $("input[name='wechat']").val(obj.weChat);
        $("input[name='birthday']").val(obj.birthday);
        $("input[name='mobile']").val(obj.mobiles[0]);
        if (obj.picture != "") {
            $('#filePicker').attr('src', r.data.picture);
        }
    }
    for (var i = 1; i < obj.mobiles.length; i++) {
        var str = '<p class="weight_out por wrap_call">' +
            '         <span onclick= "removeMobile(this)"><img src="img/mobile0.png" id="addMobile"></span>手机' +
            '         <input type="text" placeholder="必填" class="weight" maxlength="11" value= ' + obj.mobiles[i] +
            ' name="mobile" readonly></p>'
        $("#mobileTop").before(str);
    }
    callback();
}

function getHisInfo(oid, memberNum, callback) {
    var data = {
        oid: oid,
        memberNum: memberNum
    };
    $(".loading").show();
    $.ajax({
        url: $.httpRootPath() + 'rest/user/getInfo',
        type: 'GET',
        data: data,
        dataType: 'json',
        timeout: 15000,
        success: function (r) {
            $(".loading").hide();
            if (r.code == "00") {
                var obj = r.data;
                obj.mobiles = obj.mobiles.split("/");
                if (r.code == '00') {
                    for (var key in obj) {
                        $(':input[name=' + key + ']').val(obj[key]);
                    }
                    $("input[name='addr']").val(obj.site);
                    $("input[name='wechat']").val(obj.weChat);
                    $("input[name='birthday']").val(obj.birthday);
                    $("input[name='mobile']").val(obj.mobiles[0]);
                    $("input[name='departement']").val(obj.department.name);
                    $("input[name='profession']").val(obj.profession.name);
                    if (obj.picture != "") {
                        $('#filePicker').attr('src', r.data.picture);
                    }
                }
                for (var i = 1; i < obj.mobiles.length; i++) {
                    var str = '<p class="weight_out por wrap_call">' +
                        '         <span onclick= "removeMobile(this)"><img src="img/mobile0.png" id="addMobile"></span>手机' +
                        '         <input type="text" placeholder="必填" class="weight" maxlength="11" value= ' + obj.mobiles[i] +
                        ' name="mobile" readonly></p>'
                    $("#mobileTop").before(str);
                }
                callback();
            } else {
                errorToast("获取信息失败:" + r.msg, 3)
            }
        },
        error: function (e) {
            $(".loading").hide();
            ajaxErrorEvent(e);
        }
    });
}

function getOrgInfo(oid, callback) {
    var data = {
        oid: oid
    };
    $(".loading").show();
    $.ajax({
        url: $.httpRootPath() + 'rest/group/getGroup',
        type: 'GET',
        dataType: 'json',
        data: data,
        cache: false,
        timeout: 15000,
        success: function (r) {
            $(".loading").hide();
            if (r.code != "00") {
                $(".tips-error").html(r.msg).removeClass('none');
                return false;
            }
            $(".tips-error").addClass('none');
            if (callback && typeof(callback) == "function") {
                callback();
            }

        },
        error: function (e) {
            $(".loading").hide();
            ajaxErrorEvent(e);
        }
    })
}

function getParameter(type) {
    var resp = "";
    var data = {
        type: type
    };
    $.ajax({
        url: $.httpRootPath() + 'rest/sys/parameter',
        type: 'GET',
        dataType: 'json',
        data: data,
        async: false,
        cache: false,
        timeout: 15000,
        success: function (r) {
            console.log(r);
            if (r.code == "00") {
                resp = r.data;
            }
        },
    })
    return resp;
}

function addFollow(oid) {
    var data = {
        oid: oid
    };
    $(".loading").show();
    $.ajax({
        url: $.httpRootPath() + 'rest/group/join',
        type: 'POST',
        dataType: 'json',
        data: data,
        cache: false,
        timeout: 15000,
        success: function (r) {
            $(".loading").hide();
            if (r.code == "00") {
                errorToast("您关注了该组织", 3);
                $(".purchaseBtn").removeAttr("onclick")
                $(".purchaseBtn").html("已关注");
            } else if (r.code == "03") {
                errorToast("您已存在该组织", 3);
                $(".purchaseBtn").removeAttr("onclick")
                $(".purchaseBtn").html("已关注");
            } else {
                errorToast(r.msg, 3);
            }
        },
        error: function (e) {
            $(".loading").hide();
            ajaxErrorEvent(e);
        }
    })
}

function getOrgMemInfo(oid) {
    var data = {
        oid: oid
    };
    $(".loading").show();
    $.ajax({
        url: $.httpRootPath() + 'rest/user/getInfo',
        type: 'GET',
        data: data,
        dataType: 'json',
        timeout: 15000,
        success: function (r) {
            console.log(r)
            $(".loading").hide();
            if (r.code == "00") {
                var obj = r.data;
                if (r.code == '00') {
                    var type = obj.type;
                    var memS = obj.memAuth == "0" ? "信息已授权" : "信息未授权";
                    var orgS = obj.orgAuth == "0" ? "信息已授权" : "信息未授权";
                    typeName = "";
                    if (type == "0") {
                        typeName = "私有组织"
                    } else if (type == "1") {
                        typeName = "分享组织";
                    } else {
                        typeName = "公共组织";
                    }
                    if (r.data.picture != "") {
                        $('#filePicker').attr('src', r.data.picture);
                    }
                    $("#authorizeMemState").html(memS);
                    $("#authorizeOrgState").html(orgS);
                    $("#orgMark").html(obj.mark);
                    $("#memberNum").html(obj.memberNum);
                    $("#delayType").html(typeName + " · " + "组织信息");
                    $(".queryText").html(obj.name);
                    $("#depName").html("所属部门 · " + obj.depName);
                    $("#proName").html("拥有职称 · " + obj.proName);
                    $("#joinTime").html("加入时间 · " + obj.joinTime);
                    $("#tenueTime").html("成立时间 · " + obj.tenueTime);
                    $(".organizeType").val(type);
                    $(".memberNum").val(obj.memberNum);
                }
            } else {
                errorToast("获取信息失败:" + r.msg, 3)
            }
        },
        error: function (e) {
            $(".loading").hide();
            ajaxErrorEvent(e);
        }
    });
}

function updateAuthorize(oid, type, callback) {
    var data = {
        oid: oid,
        type: type,
    };
    $(".loading").show();
    $.ajax({
        url: $.httpRootPath() + 'rest/group/authorize',
        type: 'POST',
        dataType: 'json',
        data: data,
        timeout: 15000,
        cache: false,
        success: function (r) {
            $(".loading").hide();
            var tips = "";
            if (r.code == "00") {
                if (type == 'orgAuth') {
                    if (r.data.orgAuth == "1") {
                        tips = "管理员不可查看你的信息"
                    } else {
                        tips = "管理员可查看你的信息"
                    }
                } else {
                    if (r.data.memAuth == "1") {
                        tips = "其他成员不可查看你的信息"
                    } else {
                        tips = "其他成员可查看你的信息"
                    }
                }
                alert_LHX({
                    hasBtn: 1,
                    msg: "授权状态更新成功" + "<br>" + tips,
                })
            } else {
                errorToast(r.msg, 3);
            }
        },
        error: function (e) {
            $(".loading").hide();
            ajaxErrorEvent(e);
        },
        complete: function () {
            callback();
        }
    });
}

function updateDriverSubmit(data) {
    console.log('step into registerSubmit()')
    $(".loading").show();
    $.ajax({
        url: $.httpRootPath() + 'rest/driver/driver/updateInfo',
        type: 'POST',
        dataType: 'json',
        data: data,
        timeout: 15000,
        cache: false,
        success: function (r) {
            $(".loading").hide();
            if (r.code == "00") {
                errorToast("信息更新成功", 3);
            } else {
                errorToast(r.msg, 3);
                return;
            }
            if (fromPage != null && fromPage != "") {
                errorToast("更新成功,跳转至上个页面", 3);
                setTimeout(function () {
                    window.location.href = fromPage;
                }, 3000);
            } else {
                errorToast("更新成功", 3);
                setTimeout(function () {
                    window.location.href = "wxmp/driver/server";
                }, 3000);
            }


        },
        error: function (e) {
            $(".loading").hide();
            ajaxErrorEvent(e);
        }
    });

}

function getDriverInfo(data, callback) {
    $(".loading").show();
    $.ajax({
        url: $.httpRootPath() + 'rest/driver/driver/getInfo',
        type: 'GET',
        data: data,
        dataType: 'json',
        timeout: 15000,
        success: function (r) {
            $(".loading").hide();
            if (r.code == "00") {
                var obj = r.data;
                $("input[name='name']").val(obj.name);
                $("input[name='dossier']").val(obj.dossier);
                $("input[name='licenseno']").val(obj.licenseNo);
                $("input[name='email']").val(obj.email);
                $("input[name='mobile']").val(obj.mobile);
                $("#classmodel option[value='" + obj.classModel + "']").attr("selected", "selected");
                if (obj.picture != "") {
                    $('#filePicker').attr('src', r.data.picture);
                }
            } else {
                errorToast("获取信息失败:" + r.msg, 3)
            }
        },
        error: function (e) {
            $(".loading").hide();
            ajaxErrorEvent(e);
        },
        complete: function () {
            callback();
        }
    });
}
function updateVehicleSubmit(data) {
    console.log('step into registerSubmit()')
    $(".loading").show();
    $.ajax({
        url: $.httpRootPath() + 'rest/driver/vehicle/updateInfo',
        type: 'POST',
        dataType: 'json',
        data: data,
        timeout: 15000,
        cache: false,
        success: function (r) {
            $(".loading").hide();
            if (r.code == "00") {
                errorToast("信息更新成功", 3);
            } else {
                errorToast(r.msg, 3);
                return;
            }
            if (fromPage != null && fromPage != "") {
                errorToast("更新成功,跳转至上个页面", 3);
                setTimeout(function () {
                    window.location.href = fromPage;
                }, 3000);
            } else {
                errorToast("更新成功", 3);
                setTimeout(function () {
                    window.location.href = "wxmp/driver/server";
                }, 3000);
            }


        },
        error: function (e) {
            $(".loading").hide();
            ajaxErrorEvent(e);
        }
    });

}
function getVehicleInfo(data, callback) {
    $(".loading").show();
    $.ajax({
        url: $.httpRootPath() + 'rest/driver/vehicle/getInfo',
        type: 'GET',
        data: data,
        dataType: 'json',
        timeout: 15000,
        success: function (r) {
            $(".loading").hide();
            if (r.code == "00") {
                var obj = r.data;
                $("input[name='name']").val(obj.name);
                $("input[name='dossier']").val(obj.dossier);
                $("input[name='licenseno']").val(obj.licenseNo);
                $("input[name='email']").val(obj.email);
                $("input[name='mobile']").val(obj.mobile);
                $("#classmodel option[value='" + obj.classModel + "']").attr("selected", "selected");
                if (obj.picture != "") {
                    $('#filePicker').attr('src', r.data.picture);
                }
            } else {
                errorToast("获取信息失败:" + r.msg, 3)
            }
        },
        error: function (e) {
            $(".loading").hide();
            ajaxErrorEvent(e);
        },
        complete: function () {
            callback();
        }
    });
}
function updateBirthdaySubmit(data) {
    console.log('step into registerSubmit()')
    $(".loading").show();
    $.ajax({
        url: $.httpRootPath() + 'rest/remind/birthday/updateInfo',
        type: 'POST',
        dataType: 'json',
        data: data,
        timeout: 15000,
        cache: false,
        success: function (r) {
            $(".loading").hide();
            if (r.code == "00") {
                errorToast("信息更新成功", 3);
            } else {
                errorToast(r.msg, 3);
                return;
            }
            if (fromPage != null && fromPage != "") {
                errorToast("更新成功,跳转至上个页面", 3);
                setTimeout(function () {
                    window.location.href = fromPage;
                }, 3000);
            } else {
                errorToast("更新成功", 3);
                setTimeout(function () {
                    window.location.href = "wxmp/remind/server";
                }, 3000);
            }


        },
        error: function (e) {
            $(".loading").hide();
            ajaxErrorEvent(e);
        }
    });

}
function getBirthdayInfo(data, callback) {
    $(".loading").show();
    $.ajax({
        url: $.httpRootPath() + 'rest/driver/vehicle/getInfo',
        type: 'GET',
        data: data,
        dataType: 'json',
        timeout: 15000,
        success: function (r) {
            $(".loading").hide();
            if (r.code == "00") {
                var obj = r.data;
                $("input[name='name']").val(obj.name);
                $("input[name='dossier']").val(obj.dossier);
                $("input[name='licenseno']").val(obj.licenseNo);
                $("input[name='email']").val(obj.email);
                $("input[name='mobile']").val(obj.mobile);
                $("#classmodel option[value='" + obj.classModel + "']").attr("selected", "selected");
                if (obj.picture != "") {
                    $('#filePicker').attr('src', r.data.picture);
                }
            } else {
                errorToast("获取信息失败:" + r.msg, 3)
            }
        },
        error: function (e) {
            $(".loading").hide();
            ajaxErrorEvent(e);
        },
        complete: function () {
            callback();
        }
    });
}
